package com.sixth.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.LoginmsgDao;
import com.sixth.entity.Loginmsg;
import com.sixth.entity.User;
import com.sixth.service.LogMsgService;
import com.sixth.util.CommonResult;
import com.sixth.util.TableLayUi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: carrent
 * @description:
 * @author: 高振杨
 * @create: 2022-01-15 09:34
 **/
@Service(value = "logmsgService")
public class LogMsgServiceImpl  implements LogMsgService {

@Autowired
private LoginmsgDao loginmsgDao;
    @Override
    public TableLayUi getallLogmsg(int page, int limit, String loginname, String logintime) {
        PageHelper.startPage(page,limit);
        String starttime = null;
        String endtime = null;
        if (logintime!=null&&logintime!="") {
            starttime = logintime.substring(0,19);
            endtime = logintime.substring(22,logintime.length());
        }
        List<Loginmsg> all = loginmsgDao.getAllLoginmsg(loginname, starttime, endtime);
        PageInfo<Loginmsg> pageInfo = new PageInfo<Loginmsg>(all);
        return new TableLayUi(0,"成功",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public CommonResult deletelog(Integer id) {
        int i = loginmsgDao.deleteByPrimaryKey(id);
        if (i>0){
            return new CommonResult(2000,"删除成功",null);
        }
        return  new CommonResult(5000,"删除失败",null);

    }

    @Override
    public CommonResult batch(String log) {
        List<Loginmsg> loginmsgs = JSONArray.parseArray(log, Loginmsg.class);
        Loginmsg loginmsg = new Loginmsg();
        for (int i = 0; i < loginmsgs.size(); i++) {
            loginmsg.setId(loginmsgs.get(i).getId());
            Integer logid = loginmsg.getId();
            int j = loginmsgDao.dels(logid);
            if (j == 0) {
                return new CommonResult(5000, "操作失败", null);
            }
        }
        return new CommonResult(2000,"操作成功",null);
    }
}
